package io;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;

/**
 * Created by fanzhenyu02 on 2020/7/4.
 */
public class Writer {

    /**
     * 写入文件（追加方式：StandardOpenOption.APPEND）
     *
     * @param filePath
     * @param content
     */
    public static void write(String filePath, String content) {
        try {
            Files.write(Paths.get(filePath), content.getBytes(StandardCharsets.UTF_8),
                    StandardOpenOption.APPEND);
        } catch (IOException e) {
            System.err.println("writing error! " + e);
        }
    }

    /**
     * 读取文件
     *
     * @param filePath
     * @param content
     */
    public static void read(String filePath, String content) {
        byte[] data = new byte[0];
        try {
            data = Files.readAllBytes(Paths.get(filePath));
            content = new String(data, StandardCharsets.UTF_8);
        } catch (IOException e) {
            System.err.println("read error!");
        }
    }

    public static void main(String[] args) {
        write("/Users/vonmax/Downloads/test.result.txt", "first test!");
        write("/Users/vonmax/Downloads/test.result.txt", "second test!");
    }


}
